package cn.offcn.controller;

import cn.offcn.entity.EmpRole;
import cn.offcn.entity.Employee;
import cn.offcn.entity.EmployeeVo;
import cn.offcn.entity.Sources;
import cn.offcn.service.EmployeeService;
import cn.offcn.service.SourcesService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author yuliantao
 * @create 2020 -09 -2020-09-21 21:51
 * @description 功能描述
 */
@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService ;

    @Autowired
    private SourcesService sourcesService;

    @ResponseBody
    @RequestMapping("/getAllDeptAndRole")
    public Map<String, Object> getAllDeptAndRole(){
            return employeeService.getAllDeptAndRole();
    }

    @ResponseBody
    @RequestMapping("/addEmployee")
    public OAResult addEmployee(Employee employee, int roleid, MultipartFile uploadPic){
        return employeeService.addEmployee(employee,roleid,uploadPic);
    }
    @ResponseBody
    @RequestMapping("/getEmployeesInfo")
    public TableVo<EmployeeVo> getEmployeesInfo(int page, int limit){
        return employeeService.getEmployeesInfo(page,limit);
    }

    @ResponseBody
    @RequestMapping("/getEmployeeByEid")
    public Map<String,Object>  getEmployeeByEid(int eid){
        return employeeService.getEmployeeByEid(eid);
    }

    @ResponseBody
    @RequestMapping("/updateEmployeeByEid")
    public OAResult  updateEmployeeByEid(Employee employee,int roleid,MultipartFile uploadPic){

        return employeeService.updateEmployeeByEid(employee,roleid,uploadPic);
    }

    @ResponseBody
    @RequestMapping("/deleteEmployeeByEids")
    public OAResult  deleteEmployeeByEids(@RequestParam("eids[]") int [] eids){

        return employeeService.deleteEmployeeByEids(eids);
    }

    @ResponseBody
    @RequestMapping("/checkLogin")
    public OAResult  checkLogin(String username, String password, HttpSession session){

        OAResult oaResult = employeeService.checkLogin(username, password);
        if (oaResult.getStatus()==200){
            session.setAttribute("activeEmployee",oaResult.getData());

        }
        return oaResult;
    }


    @RequestMapping("/getCurrentEmployeeSources")
    public String  getCurrentEmployeeSources(@RequestParam(name="pid",defaultValue = "1") int pid, HttpSession session){

        Employee employee = (Employee)session.getAttribute("activeEmployee");

        List<Sources> sourcesList = sourcesService.getSourcesByCache(employee.getEid(), pid);
        session.setAttribute("sourcesList",sourcesList);

        return "index";
    }

}
